package com.bdjk.bdmw.InfoGath.business.heremi.dao.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.bdjk.bdmw.InfoGath.business.heremi.dao.HereMiPositionDAO;
import com.bdjk.bdmw.InfoGath.business.heremi.model.HereMiPositionModel;
import com.bdjk.bdmw.InfoGath.business.heremi.vo.HereMiPositionVO;
import com.bdjk.bdmw.InfoGath.business.iparent.model.IparentPositionModel;
import com.bdjk.bdmw.InfoGath.common.dao.impl.JdbcDAOImpl;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;

public class HereMiPositionDAOImpl extends JdbcDAOImpl implements HereMiPositionDAO{


	
	public int savePosition(HereMiPositionModel model) {
		StringBuffer sql=new StringBuffer();
		sql.append("insert into t_position (ter_ident,send_time,longitude,latitude,altitude,speed,direction)values(?,?,?,?,?,?,?)");
		List<Object> params = new ArrayList<Object>();
		params.add(model.getTerIdent());
		params.add(model.getSendTime());
		params.add(model.getLongitude());
		params.add(model.getLatitude());
        params.add(model.getAltitude());
		params.add(model.getSpeed());
		params.add(model.getDirection());
		int positionId=0;
		try {
			positionId=executeSQL(sql.toString(), params);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return positionId;
	}

	
	public List<HereMiPositionModel> queryPosition(List<Object> paramList) throws BusinessException {
		String sql=new String("SELECT * FROM t_position WHERE UNIX_TIMESTAMP( send_time ) BETWEEN UNIX_TIMESTAMP( ?) AND UNIX_TIMESTAMP( ? ) AND ter_ident=?");
		List<Object> sqlrs=executeResultSQL(sql.toString(),paramList);
		List<HereMiPositionModel> result=new ArrayList<HereMiPositionModel>();
		for(int i=0;i<sqlrs.size();i++){
			HereMiPositionVO vo=new HereMiPositionVO();
			List<Object> rs=(List<Object>)sqlrs.get(i);
			int col=0;
			vo.setId(rs.get(col).toString());col++;
			vo.setTerIdent(rs.get(col).toString());col++;
			vo.setSendTime(rs.get(col).toString());col++;
			vo.setLongitude(rs.get(col).toString());col++;
			vo.setLatitude(rs.get(col).toString());col++;
			vo.setAltitude(rs.get(col).toString());col++;
			vo.setSpeed(rs.get(col).toString());col++;
			vo.setDirection(rs.get(col).toString());col++;
			
		
			//vo.setUpdateTime((Date)rs.get(col));col++;
			HereMiPositionModel model=new HereMiPositionModel();
			try {
				model = vo.vo2Model();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result.add(model);
		}
		
		return result;
	}
    
	public List<HereMiPositionModel> query(String sql,List<Object> paramList) throws BusinessException {
		List<HereMiPositionModel> result=new ArrayList<HereMiPositionModel>();
		List<Object> sqlrs=executeResultSQL(sql.toString(),paramList);
		for(int i=0;i<sqlrs.size();i++){
			HereMiPositionVO vo=new HereMiPositionVO();
			List<Object> rs=(List<Object>)sqlrs.get(i);
			int col=0;
			vo.setId(rs.get(col).toString());col++;
			vo.setTerIdent(rs.get(col).toString());col++;
			vo.setSendTime(rs.get(col).toString());col++;
			vo.setLongitude(rs.get(col).toString());col++;
			vo.setLatitude(rs.get(col).toString());col++;
			vo.setAltitude(rs.get(col).toString());col++;
			vo.setSpeed(rs.get(col).toString());col++;
			vo.setDirection(rs.get(col).toString());col++;

			HereMiPositionModel model=new HereMiPositionModel();
			try {
				model = vo.vo2Model();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result.add(model);
		}
		return result;
	}
	
	
	public HereMiPositionModel queryCurrentPosition(List<Object> paramList) throws BusinessException {
		
		String sql="SELECT * FROM (SELECT *  FROM t_position WHERE ter_ident=?)t ORDER BY send_time DESC LIMIT 0,1";
			
		HereMiPositionModel model=null;
		List<HereMiPositionModel> result=query(sql,paramList);
		if(result.size()>0){
			model=result.get(0);
		}
		return model;
	}


	@Override
	public int saveExtension(HereMiPositionModel model,int id) {
		// TODO Auto-generated method stub
		StringBuffer sql=new StringBuffer();
		sql.append("insert into t_position_extension (position_id,ter_ident,sendTime,pro_code,is_echoLocation,satellite_num,gsm_sign_strength,electricity,step_num,roll_num,ter_status,hm_base_station_num,hm_conn_bs,hm_mcc_code,hm_mnc_code,hm_area_code,hm_conn_bs_code,hm_conn_bs_strength,hm_update_time)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		params.add(model.getTerIdent());
		params.add(model.getSendTime());
		params.add(model.getProCode());
		params.add(model.getIsEchoLocation());
		params.add(model.getSatelliteNum());
        params.add(model.getGsmSignStrength());
        
		params.add(model.getElectricity());
		params.add(model.getStepNum());
		params.add(model.getRollNum());
		params.add(model.getTerStatus());
		params.add(model.getBaseStationNum());
		params.add(model.getConnBs());
		
        params.add(model.getMccCode());
		params.add(model.getMncCode());
		params.add(model.getAreaCode());
		params.add(model.getConnBsCode());
		params.add(model.getConnBsStrength());
		params.add(model.getUpdateTime());
		try {
			executeSQL(sql.toString(), params);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
